package tests;

/**
 * @author 李夏
 * @version 1.0
 */

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.lang.reflect.Method;
import java.time.Duration;

/**
 * 个人中心页面测试
 */
public class UserCenterPage extends Utils {

    public static String url = "http://127.0.0.1:58080/index.html";
    public UserCenterPage() {
        super(url);
    }

    /*
    检查页面是否加载成功
     */
    public void  userCenterPageRight() throws InterruptedException, IOException {
        LoginPage loginPage=new LoginPage();
        loginPage.loginSuc();
        WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement searchBox=wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#index_nav_nickname")));
//        System.out.println("准备点击个人中心");
        searchBox.click();
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#index_user_settings"))).click();
//        System.out.println("跳转到个人中心");
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div.row.align-items-center > div:nth-child(2) > a"));
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
    }
    /*
    输入密码是否以密文形式显示
     */
    public void changePsd() throws IOException, InterruptedException {
        userCenterPageRight();//首先确保是在个人中心界面
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("123456");
        String text = driver.findElement(By.cssSelector("#settings_input_oldPassword")).getText();
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
        assert text!="123456":"密码不能为明文";
//        System.out.println("断言通过，密码不是明文");

    }
    /*
    原密码输入正确，新密码与确认密码一致，修改成功,跳转至登录页面
     */
    public void changePsdSuc() throws IOException, InterruptedException {
        userCenterPageRight();//首先确保是在个人中心界面
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("123456");
//        System.out.println("输入了原密码");
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123456");
//        System.out.println("输入了新密码");

        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("123456");
//        System.out.println("重新输入了新密码");
        // 创建 JavaScriptExecutor 实例
        JavascriptExecutor js = (JavascriptExecutor) driver;
        // 执行 JavaScript 代码滚动到页面底部
        js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
       Thread.sleep(1000);

       driver.findElement(By.cssSelector("#settings_submit_password")).click();
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
//        System.out.println("点击确认");
        //验证是否跳转至登录页面
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2"));

    }
    /*
    原密码正确，新密码和确认密码不一致，修改失败，给出错误提示
     */
    public void changePadFail01() throws IOException, InterruptedException {
        userCenterPageRight();//首先确保是在个人中心界面
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("1231111111111");
        // 创建 JavaScriptExecutor 实例
        JavascriptExecutor js = (JavascriptExecutor) driver;
        // 执行 JavaScript 代码滚动到页面底部
        js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        //验证是否还在个人中心页面
        String currentUrl = driver.getCurrentUrl();
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
        assert currentUrl.equals("http://127.0.0.1:58080/index.html");
    }
    /*
    原密码输入错误，新密码和确认密码一致，修改失败，给出错误提示
     */
    public void changePadFail02() throws IOException, InterruptedException {
        userCenterPageRight();//首先确保是在个人中心界面
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("12341111111");
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("123456");
        // 创建 JavaScriptExecutor 实例
        JavascriptExecutor js = (JavascriptExecutor) driver;
        // 执行 JavaScript 代码滚动到页面底部
        js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        //验证是否还在个人中心页面
        String currentUrl = driver.getCurrentUrl();
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
        assert currentUrl.equals("http://127.0.0.1:58080/index.html");

    }
    /*
    昵称、头像、用户名、邮箱地址、性别、电话号码、简介都为空，修改失败，给出错误提示
     */
    public  void changePerInfoFail() throws IOException, InterruptedException {
        userCenterPageRight();//首先确保是在个人中心界面
        // 创建 JavaScriptExecutor 实例
        JavascriptExecutor js = (JavascriptExecutor) driver;
        // 执行 JavaScript 代码滚动到页面底部
        js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        //验证是否还在个人中心页面
        String currentUrl = driver.getCurrentUrl();
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
        assert currentUrl.equals("http://127.0.0.1:58080/index.html");
    }
    /*
    任意修改内容格式正确，修改成功
     */
    public void changePerInfoSuc() throws IOException, InterruptedException {
        userCenterPageRight();//首先确保是在个人中心界面
        driver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys("小新");
        driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        driver.findElement(By.cssSelector("#setting_input_email")).sendKeys("123456");
        driver.findElement(By.cssSelector("#setting_submit_email")).click();
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys("123456");
        driver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("123456");
        // 创建 JavaScriptExecutor 实例
        JavascriptExecutor js = (JavascriptExecutor) driver;
        // 执行 JavaScript 代码滚动到页面底部
        js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
        //验证是否跳转至登录页面
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2"));
    }
}
